﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Smartmanufact
{
    public partial class customer_manager : Form
    {
        public customer_manager()
        {
            InitializeComponent();
            Read_DB_Fill_Grid("customertable",dataGridView1);
            Set_Color(dataGridView1);
        }

        private void Read_DB_Fill_Grid(string table_name, DataGridView datagridview1)
        {
            // 用数据库将表格填满
            try
            {
                DataSet ds = MainView.mysql.sql_search_database("Select * from " + table_name);
                int colnum = ds.Tables[0].Columns.Count;         // 列数
                datagridview1.ColumnCount = colnum;              // 设置列数
                int rownum = ds.Tables[0].Rows.Count;            // 行数
                datagridview1.RowCount = rownum;                 // 设置行数
                for (int i = 0; i < rownum; i++)
                {
                    for (int j = 0; j < colnum; j++)
                    {
                        datagridview1[j, i].Value = ds.Tables[0].Rows[i][j].ToString();     // 把那一行显示出来
                    }
                }
            }
            catch
            {
                for (int i = 0; i < dataGridView1.RowCount; i++)
                {
                    for (int j = 0; j < dataGridView1.ColumnCount; j++)
                    {
                        datagridview1[j, i].Value = "";     // 把那一行显示出来
                    }
                }
            }
        }

        private void customer_manager_Load(object sender, EventArgs e)
        {

        }
        private void Set_Color(DataGridView datagridview1)
        {
            for (int i = 0; i < datagridview1.RowCount; i++)
            {
                for (int j = 0; j < datagridview1.ColumnCount; j++)
                {
                    if (i % 2 == 0) datagridview1[j, i].Style.BackColor = Color.LightBlue;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ADD_customer view = new ADD_customer();
            view.ShowDialog();
            Read_DB_Fill_Grid("customertable", dataGridView1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                int selected_index = dataGridView1.SelectedCells[0].RowIndex;
                string id = dataGridView1[0, selected_index].Value.ToString().Trim();
                string name = dataGridView1[1, selected_index].Value.ToString().Trim();
                string company = dataGridView1[2, selected_index].Value.ToString().Trim();
                string phone = dataGridView1[3, selected_index].Value.ToString().Trim();
                string email = dataGridView1[4, selected_index].Value.ToString().Trim();
                CutomerEdit cutomerView = new CutomerEdit();
                cutomerView.origincutsomer_id = id;
                cutomerView.originname = name;
                cutomerView.origincompany = company;
                cutomerView.originphone = phone;
                cutomerView.originemail = email;
                cutomerView.Fill_info();
                cutomerView.ShowDialog();
                Read_DB_Fill_Grid("customertable", dataGridView1);
            }
            catch { }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                int selected_index = dataGridView1.SelectedCells[0].RowIndex;
                string id = dataGridView1[0, selected_index].Value.ToString().Trim();
                DeleteCutmoerconfig view = new DeleteCutmoerconfig();
                view.originid = id;
                view.ShowDialog();
                Read_DB_Fill_Grid("customertable", dataGridView1);
            }
            catch { }
        }
    }
}
